// Aachen Flursteuerung

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#define maxLength 50

byte mac[] = {
  0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed }; //update with your MAC rnd
byte ip[] = {
  192, 168, 178, 18 };
String inString = String(maxLength);
int val;
int r = 0;
int g = 0;
int b = 0;
int f = 0;
int k = 0;
int l = 0;
int KloStatus=0;
char colorBuff[4];
Server server(80);

void setup()
{

  pinMode(3, OUTPUT);      // R  Flur
  pinMode(9, OUTPUT);      // G  Flur  
  pinMode(6, OUTPUT);      // B  Flur
  
  pinMode(2, INPUT);       // Input Klo Kontakt
  pinMode(4, OUTPUT);      // R LED-Zeile Klo
  pinMode(5, OUTPUT);      // G LED-Zeile Klo
  pinMode(7, OUTPUT);      // B LED-Zeile Klo
  
  pinMode(8, OUTPUT);      // Tueroeffner
  
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();

  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(CS22);
  
  OCR2B = 0;  
  digitalWrite(3, LOW);
  analogWrite(9, 0);    
  analogWrite(6, 0); 

  Serial.println("- Flur Steuerung v 1.0 Initialisiert -");

}
void loop()
{ 
  KloStatus = digitalRead(2);
  
  if (KloStatus == 0){
    digitalWrite(4, LOW);            
  }else{
    digitalWrite(4, HIGH);
  }
  
  int bufLength;
  Client client = server.available();
  if (client) {
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (inString.length() < maxLength) {
          inString += c;
        }        
        if (c == '\n' && current_line_is_blank) {
          if (inString.indexOf("?") > -1) { 
            
            int Pos_r = inString.indexOf("r");
            int Pos_g = inString.indexOf("g", Pos_r);
            int Pos_b = inString.indexOf("b", Pos_g);
            int Pos_f = inString.indexOf("f", Pos_b);
            int Pos_k = inString.indexOf("k", Pos_f);
            int Pos_l = inString.indexOf("l", Pos_k);
            int End = inString.indexOf("H", Pos_l);

            if(End < 0){
              End =  inString.length() + 1;
            }
            bufLength = ((Pos_g) - (Pos_r+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }    
            inString.substring((Pos_r+2), (Pos_g-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            r = atoi(colorBuff);
            bufLength = ((Pos_f) - (Pos_g+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }      
            inString.substring((Pos_g+2), (Pos_b-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            g = atoi(colorBuff);
            bufLength = ((Pos_f) - (Pos_b+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }      
            inString.substring((Pos_b+2), (Pos_f-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            b = atoi(colorBuff);
            bufLength = ((Pos_k) - (Pos_f+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }     
            inString.substring((Pos_f+2), (Pos_k-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            f = atoi(colorBuff);
            bufLength = ((Pos_l) - (Pos_k+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }    
            inString.substring((Pos_k+2), (Pos_l-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            k = atoi(colorBuff);
            bufLength = ((End) - (Pos_l+2));
            if(bufLength > 4){  //dont overflow the buffer
              bufLength = 4;
            }   
            inString.substring((Pos_l+2), (End-1)).toCharArray(colorBuff, bufLength);  //transfer substring to buffer
            l = atoi(colorBuff);
            
            Serial.print("New Values = ");
            Serial.print( r );
            Serial.print(", ");
            Serial.print( g );
            Serial.print(", ");
            Serial.print( b );
            Serial.print(", ");
            Serial.print( f );
            Serial.print(", ");
            Serial.print( k );
            Serial.print(", ");
            Serial.println( l );
            
            // Tueroeffner
            if (f == 0){
              digitalWrite(8, LOW);
            }else{
              digitalWrite(8, HIGH);
            }
            
              if (r == 0){
                digitalWrite(3, LOW);
              }else{
                TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
                TCCR2B = _BV(CS22);
                
                OCR2B = (r);
              }  
              analogWrite(9, (g));    
              analogWrite(6, (b)); 
            
            analogWrite(5, k); 
            
            // Workaround B Flur  -> Soft PWM muss noch implementiert werden.
            if (l == 0){
              digitalWrite(7, LOW);  
            }else{
              digitalWrite(7, HIGH);  
            }

          }
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><head><h1>Chaos WG Flur Interface</h1></head><body>");
          client.println("<h3>Flur Beleuchtung (0 - 255)</h3>");
          client.print("<form method=get>R:<input type=text size=3 name=r value=");
          client.print(r);
          client.print(">G:<input type=text size=3 name=g value=");
          client.print(g);
          client.print(">B:<input type=text size=3 name=b value=");
          client.print(b);
          client.println(">&nbsp;<br><br><hr><h3>Tueroeffner</h3>");
          client.println("Tueroeffner:<input type=text size=3 name=f value=");
          client.print(f);
          client.println(">&nbsp;<br><br><hr><h3>Klo Status</h3>");
          client.println("<table border=1 width=200>");
          if (KloStatus == 0){
            client.println("<tr style=background-color:green><th>FREI !");          
          }else{
            client.println("<tr style=background-color:red><th>Besetzt !");
          }
          client.println("</th></tr></table><br>");
          client.println("G:<input type=text size=3 name=k value=");
          client.print(k);
          client.println(">&nbsp;");
          client.println("B:<input type=text size=3 name=l value=");
          client.print(l);
          client.println(">&nbsp;<br><br>");
          client.println("<input name=H type=submit value=submit></form>");
          client.println("</body></html>");
          break;
        }
        if (c == '\n') {
          current_line_is_blank = true;
        }
        else if (c != '\r') {
          current_line_is_blank = false;
        }
      }
    }
    
    delay(1);
    inString = "";
    client.stop();
  }
} 
